import threading
import os

debugging = False
# if not debug mode then don't log to file or console
# comment out the next two lines if debugging and logging to file and console.
if not debugging:
    os.environ['KIVY_NO_FILELOG'] = '1'
    os.environ['KIVY_NO_CONSOLELOG'] = '1'

import kivy
kivy.require('1.8.0')

from kivy.app import App
from kivy.config import ConfigParser
from kivy.logger import Logger
from kivy.metrics import dp, sp
from kivy.uix.settings import Settings

# from amgineirc.model.irc import IRCClientModel
from amgineirc.model.irc.ircClientModel import IRCClientModel
from amgineirc.view.appView import AppView
from amgineirc.model.irc.dccHandShakeModel import DCCHandShakeModel
from amgineirc.model.settings.persistentData import *
    

class AmgineIRCApp(App):
    """
    this is the application class.
    the build method creates and returns an AppView object.
    that AppView object calls the other views.
    those views setup thier view models.
    """
    icon = 'amgine_64.png'
    

    def build(self):
        """
        appdata is a dictionary of applicatin wide data.
        appdata is passed to classes that need it.
        """
        self.appdata = {}
        self.appdata['version'] = {'major':0, 'minor':7, 'bugfix':2}
        # this app
        self.appdata['app'] = self
        # debug mode
        self.appdata['debug'] = 'KIVY_NO_CONSOLELOG' not in os.environ
        # the quit message suffix
        self.appdata['quit_message'] = 'AmgineIRC'
        # where the persistent data is stored
        self.appdata['user_data_dir'] = os.path.realpath(self.directory)

        # persistent data

        # the server persistent data
        self.appdata['servers'] = ServersModel(self.appdata['user_data_dir'])
        # the irc nicks of people who are trusted persistent data
        self.appdata['trusted_users'] = TrustedUsersModel(self.appdata['user_data_dir'])
        # the amgine rules persistent data
        self.appdata['rules'] = RulesModel(self.appdata['user_data_dir'])

        # irc and dcc models
        
        # the irc client
        self.appdata['client'] = IRCClientModel()
        # the dcc handshaker
        dccHandShakeModel = DCCHandShakeModel(self.appdata)

        # views and view models
        
        # the view model of the view which wraps all of the server and channel and dcc connection views
        self.appdata['connection_vm'] = None
        # the view model of the trusted users view 
        self.appdata['trusted_vm'] = None
        # this app's root view
        self.appdata['app_view'] = AppView(self.appdata)

        return self.appdata['app_view']
    
    def on_start(self):
        pass

    def on_pause(self):
        pass
    
    def on_resume(self):
        pass
    
    def on_stop(self):
        self.appdata['client'].disconnect_all(self.appdata['quit_message'])

#    def build_config(self, config):
#        path = os.path.join(self.directory, 'amgineirc.ini')
#        config.read(path)
#        config.adddefaultsection('The IRC Server')
#        config.setdefault('The IRC Server', 'domain_name', '')
#        config.setdefault('The IRC Server', 'port', 6667)
#        config.adddefaultsection('Me')
#        config.setdefault('Me', 'real_name', '')
#        config.setdefault('Me', 'email', '')
#        config.setdefault('Me', 'nick', '')
#        config.setdefault('Me', 'password', '')
#        
#    def build_settings(self, settings):
#        settings.add_json_panel('The IRC Server', self.config, None, IrnocApp.server_json)
#        settings.add_json_panel('Me', self.config, None, IrnocApp.me_json)

app = AmgineIRCApp()
# Logger.info('user data at :' + app.appdata['user_data_dir'])
app.run()
